using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PharmacyDomain;
using ShivamEMR.Model.DataInterfaces.InventoryDataInterf;
using ShivamEMR.Model.DataInterfaces.PharmacyDataInterf;
using ShivamEMR.Model;
//using ShivamEMR.Web.Areas.Pharmacy.Models;

namespace ShivamEMR.Web.Areas.Pharmacy.Controllers
{   
    public class DrugItemController : Controller
    {
		private readonly IItemTypeRepository itemtypeRepository;
		private readonly IItemGroupRepository itemgroupRepository;
		private readonly IManufacturerRepository manufacturerRepository;
		private readonly IItemPictureRepository itempictureRepository;
		private readonly IItemStatusRepository itemstatusRepository;
		private readonly IDrugRepository drugRepository;
		private readonly IDrugItemRepository drugitemRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public DrugItemController()
        {
            itemtypeRepository= Registry.RepositoryFactory.GetItemTypeRepository();
                itemgroupRepository= Registry.RepositoryFactory.GetItemGroupRepository();
                manufacturerRepository= Registry.RepositoryFactory.GetManufacturerRepository();
                    itempictureRepository= Registry.RepositoryFactory.GetItemPictureRepository();
                    itemstatusRepository= Registry.RepositoryFactory.GetItemStatusRepository();
                        drugRepository= Registry.RepositoryFactory.GetDrugRepository();
                        drugitemRepository = Registry.RepositoryFactory.GetDrugItemRepository();
        }

        public DrugItemController(IItemTypeRepository itemtypeRepository, IItemGroupRepository itemgroupRepository, IManufacturerRepository manufacturerRepository, IItemPictureRepository itempictureRepository, IItemStatusRepository itemstatusRepository, IDrugRepository drugRepository, IDrugItemRepository drugitemRepository)
        {
			this.itemtypeRepository = itemtypeRepository;
			this.itemgroupRepository = itemgroupRepository;
			this.manufacturerRepository = manufacturerRepository;
			this.itempictureRepository = itempictureRepository;
			this.itemstatusRepository = itemstatusRepository;
			this.drugRepository = drugRepository;
			this.drugitemRepository = drugitemRepository;
        }

        //
        // GET: /DrugItem/

        public ViewResult Index()
        {
        //    return View(drugitemRepository.AllIncluding(drugitem => drugitem.ItemType, drugitem => drugitem.Manufacturer, drugitem => drugitem.Picture, drugitem => drugitem.Prices, drugitem => drugitem.Drug));
            return View(drugitemRepository.All);
        }

        //
        // GET: /DrugItem/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(drugitemRepository.Find(id));
        }

        //
        // GET: /DrugItem/Create

        public ActionResult Create()
        {
			ViewBag.PossibleItemTypes = itemtypeRepository.All;
			ViewBag.PossibleItemGroups = itemgroupRepository.All;
			ViewBag.PossibleManufacturers = manufacturerRepository.All;
			ViewBag.PossiblePictures = itempictureRepository.All;
			ViewBag.PossibleItemStatus = itemstatusRepository.All;
			ViewBag.PossibleDrugs = drugRepository.All;
            return View();
        } 

        //
        // POST: /DrugItem/Create

        [HttpPost]
        public ActionResult Create(DrugItem drugitem)
        {
            if (ModelState.IsValid) {
                drugitemRepository.InsertOrUpdate(drugitem);
                drugitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleItemTypes = itemtypeRepository.All;
				ViewBag.PossibleItemGroups = itemgroupRepository.All;
				ViewBag.PossibleManufacturers = manufacturerRepository.All;
				ViewBag.PossiblePictures = itempictureRepository.All;
				ViewBag.PossibleItemStatus = itemstatusRepository.All;
				ViewBag.PossibleDrugs = drugRepository.All;
				return View();
			}
        }
        
        //
        // GET: /DrugItem/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleItemTypes = itemtypeRepository.All;
			ViewBag.PossibleItemGroups = itemgroupRepository.All;
			ViewBag.PossibleManufacturers = manufacturerRepository.All;
			ViewBag.PossiblePictures = itempictureRepository.All;
			ViewBag.PossibleItemStatus = itemstatusRepository.All;
			ViewBag.PossibleDrugs = drugRepository.All;
             return View(drugitemRepository.Find(id));
        }

        //
        // POST: /DrugItem/Edit/5

        [HttpPost]
        public ActionResult Edit(DrugItem drugitem)
        {
            if (ModelState.IsValid) {
                drugitemRepository.InsertOrUpdate(drugitem);
                drugitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleItemTypes = itemtypeRepository.All;
				ViewBag.PossibleItemGroups = itemgroupRepository.All;
				ViewBag.PossibleManufacturers = manufacturerRepository.All;
				ViewBag.PossiblePictures = itempictureRepository.All;
				ViewBag.PossibleItemStatus = itemstatusRepository.All;
				ViewBag.PossibleDrugs = drugRepository.All;
				return View();
			}
        }

        //
        // GET: /DrugItem/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(drugitemRepository.Find(id));
        }

        //
        // POST: /DrugItem/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var drugitem = drugitemRepository.Find(id);
            drugitemRepository.Delete(drugitem);
            drugitemRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

